﻿using Microsoft.TeamFoundation;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TFSUtilLibrary
{
    public class Connection
    {
        private TfsTeamProjectCollection _tfs;
        public string URI { get; set; }

        public VersionControlServer VCS { get; set; }
        public IBuildServer BuildServer { get; set; }

        public Connection(string uri)
        {
            URI = uri;
        }

        public bool ConnectToTFS()
        {
            try
            {
                _tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(URI));

                _tfs.Connect(Microsoft.TeamFoundation.Framework.Common.ConnectOptions.IncludeServices);                

                VCS = _tfs.GetService<VersionControlServer>();                
                BuildServer = _tfs.GetService<IBuildServer>();                

                return true;
            }
            catch 
            {
                return false;
            }
        }
    }
}
